function TreeView (config){
	
	/** self pointer */
	var _this = this;
	
	this.treeDataPath = "/base/list/tree-data";
	
	/** Init function */
	this.init = function(){
		
		var args = config.dataset.split(/\./);
		var catalog = args[0];
		var entity = args[1];
		var entityParam = "?nebula_req_param_treeid="+config.divid+"&nebula_req_param_catalog="+catalog+"&nebula_req_param_entity="+entity;
		
		ajaxGet(nebula.basePath + _this.treeDataPath + entityParam, {}, function(records){
			
			$('#'+config.divid).jstree({
				'core' : {
					'data' : _this.getTreeJson(records, config),
					'expand_selected_onload':true
				},
				'plugins' : ["wholerow"],
				'types' : {
					"#" : {
						"max_children" : 1, 
						"max_depth" : 4
					}
				}
			});
			
			if(config.type == "treeselect"){
				var treeSelect = new TreeSelect('#'+config.divid);
				treeSelect.createTreeSelect();
			}else{
				$('#'+config.divid).on("changed.jstree", function (e, data) {
					var selectedNode = data.instance.get_node(data.selected);
					var id = selectedNode.id.split("_")[1];
					eval(config.action)(id);
				});
			}
		});
	};
	
	this.getTreeJson = function(records,config){
		
		var treeJson = new Array();
		
		for (var j = 0; j < records.length; j++) {
			var treeNodeJson = {};
			treeNodeJson["text"] = records[j][config.text];
			treeNodeJson['id'] = config.divid + "_" + records[j][config.treeId];
			treeNodeJson['state'] = {opened:config.opened};
			if(records[j].childNodes != null){
				treeNodeJson['children'] = _this.getTreeJson(records[j].childNodes,config);
			}
			
			treeJson.push(treeNodeJson);
		}
		
		return treeJson;
	};

	
	this.init();
}

function TreeSelect(id){
	
	/** self pointer */
	var _this = this;
	this.textObj;
	this.hiddenObj;
	this.divObj;
	
	/** Init function */
	this.init = function(){
		_this.divObj = $(id);
		_this.textObj = _this.divObj.closest("div.treeselect").find("> input[type='text']");
		_this.hiddenObj = _this.divObj.closest("div.treeselect").find("> input[type='hidden']");
	};
	
	this.init();
	
	this.createTreeSelect = function(){
		
		_this.divObj.on("click", function (e, data) {
			e.stopPropagation();
		});
		
		_this.divObj.on("changed.jstree", function (e, data) {
			var selectedNode = data.instance.get_node(data.selected);
			_this.textObj.val(selectedNode.text);
			var id = selectedNode.id;
			_this.hiddenObj.val(id.split("_")[1]);
			_this.divObj.parent().removeClass("open");
		});
	};
}